<?php

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

//$factory->define(App\User::class, function (Faker\Generator $faker) {
//    return [
//        'name' => $faker->name,
//        'email' => $faker->safeEmail,
//        'password' => '123456',
//        'remember_token' => str_random(10),
//    ];
//});

use App\Models\Scope\CompanyScope;

$factory->define(App\Models\SyUser::class, function (Faker\Generator $faker) {
    return [
        'username' => $faker->name,
        'userpwd' => '123456',
    ];
});
$factory->define(App\Models\Phpwarn::class, function (Faker\Generator $faker) {
    
    $devices = App\Models\PriDeviceInfo::withoutGlobalScope(CompanyScope::class)
            ->select(['pdi_index', 'dpt_id', 'pdi_name', 'AreaId', 'Co_ID'])
            ->get()
            ->toArray();
    $device = $faker->randomElement($devices);
    
    $type = App\Models\DeviceType::where('dt_typeid', '=', array_get($device, 'dpt_id'))->first();
    
    $area = App\Models\Area::where('AreaId', '=', array_get($device, 'AreaId'))->first();
    
    $warns = App\Models\WarnDefine::where('dt_typeid', array_get($device, 'dpt_id'))->select(['wd_warndesc0'])->get()
            ->map(function($item){
                return $item['wd_warndesc0'];
            });
            
    if($warns->count()>0) {
        $warn = $warns->random();
    }else {
        $warn = $faker->sentence();
    }
    $dates = [];
    foreach (range(1, 2) as  $v) {
        $dates[] = date('Y-m-d H:i:s', strtotime("-{$v} days"));
    }
    $dates[] = date('Y-m-d H:i:s');
    return [
        'pdi_devid' => array_get($device, 'pdi_index'),
        'pdi_name' => array_get($device, 'pdi_name'),
        'pdi_type'=> array_get($device, 'dpt_id'),
        'pdi_typename'=>array_get($type, 'dt_typename'),
        'pdi_typememo'=>array_get($type, 'dt_typememo'),
        'pdi_warnname'=>$warn,
        'pdi_areaname'=>array_get($area, 'AreaName'),
        'pdi_areaid'=>array_get($device, 'AreaId'),
        'pdi_realtable'=>array_get($type, 'dt_rtdata_table'),
        'pdi_histable'=>array_get($type, 'dt_hisdata_table'),
        'pdi_updatetime'=>$faker->randomElement($dates),
        'pdi_realdata'=>$faker->word(),
        'pdi_realdataname'=>$faker->word(),
        'pdi_realdatadesc'=>$faker->sentence(),
        'pdi_realdataunit'=>$faker->title(),
        'pdi_MoniType'=>0,
        'pdi_warnlevel'=>$faker->randomElement(range(1, 4)),
        'Co_ID'=>array_get($device, 'Co_ID'),
    ];
});


$factory->define(App\Models\WaterAff::class, function (Faker $faker) {
    return [
        'username' => $faker->unique()->safeEmail,
        'userpwd' => '123456', // secret
    ];
});